package com.starsoft.smdc.crawler.netease;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.frame.fileimport.JsonReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcHandicap;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcMarketDailyId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.common.HandicapCrawler;
import com.starsoft.smdc.util.SecurityType;


public class HandicapNetease extends NeteaseService implements HandicapCrawler{

	private static Logger logger = LoggerFactory.getLogger(HandicapNetease.class);

	@Override
	public SmdcHandicap getHandicap(SmdcSecurity security) {
		List<SmdcHandicap> handicapList=  this.getHandicap(Arrays.asList(security));
		if(handicapList.size()>0) return handicapList.get(0);
		return null;
	}
	
	@Override
	public List<SmdcHandicap> getHandicap(List<SmdcSecurity> securityList) {
		List<SmdcHandicap> handicapList = new ArrayList<SmdcHandicap>();
		if(securityList==null || securityList.isEmpty()){
			return handicapList;
		}
		
		String secIds = "";
		for(SmdcSecurity security:securityList){
			secIds += convertSecId(security.getSecId(), false)+",";
		}
		return getHandicap(secIds);
	}
	
	private List<SmdcHandicap> getHandicap(String secIds) {
		List<SmdcHandicap> handicapList = new ArrayList<SmdcHandicap>();
		String apiName = "handicap";
		String response = this.request(apiName, secIds);
		if(StringUtil.isEmpty(response)){
			throw new RuntimeException(secIds+"netease handicap no response");
		}
		List<Map<String, Object>> listValue = parseToList(response);
		if(listValue.size()==0) {
			logger.warn(secIds+"netease handicap no response");
			return handicapList;
		}
		
		List<Column> columnList = new ArrayList<Column>();
		columnList.add(new Column("symbol", "ticker", ValueType.String, NullType.notnull));
		columnList.add(new Column("code", "secId", ValueType.String, NullType.notnull));
		columnList.add(new Column("price", "price", ValueType.Double, NullType.nullable));
		columnList.add(new Column("open", "openPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("yestclose", "preClosePrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("high", "highPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("low", "lowPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("percent", "percent", ValueType.Double, NullType.nullable));
		columnList.add(new Column("updown", "priceChange", ValueType.Double, NullType.nullable));
		columnList.add(new Column("volume", "turnoverVol", ValueType.Double, NullType.nullable));
		columnList.add(new Column("turnover", "turnoverValue", ValueType.Double, NullType.nullable));
		columnList.add(new Column("turnoverRate", "turnoverRate", ValueType.Double, NullType.nullable));

		columnList.add(new Column("bid1", "buy1", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bid2", "buy2", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bid3", "buy3", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bid4", "buy4", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bid5", "buy5", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bidvol1", "buyVol1", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bidvol2", "buyVol2", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bidvol3", "buyVol3", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bidvol4", "buyVol4", ValueType.Double, NullType.nullable));
		columnList.add(new Column("bidvol5", "buyVol5", ValueType.Double, NullType.nullable));

		columnList.add(new Column("ask1", "sell1", ValueType.Double, NullType.nullable));
		columnList.add(new Column("ask2", "sell2", ValueType.Double, NullType.nullable));
		columnList.add(new Column("ask3", "sell3", ValueType.Double, NullType.nullable));
		columnList.add(new Column("ask4", "sell4", ValueType.Double, NullType.nullable));
		columnList.add(new Column("ask5", "sell5", ValueType.Double, NullType.nullable));
		columnList.add(new Column("askvol1", "sellVol1", ValueType.Double, NullType.nullable));
		columnList.add(new Column("askvol2", "sellVol2", ValueType.Double, NullType.nullable));
		columnList.add(new Column("askvol3", "sellVol3", ValueType.Double, NullType.nullable));
		columnList.add(new Column("askvol4", "sellVol4", ValueType.Double, NullType.nullable));
		columnList.add(new Column("askvol5", "sellVol5", ValueType.Double, NullType.nullable));

		columnList.add(new Column("time", "updateTime", ValueType.Date, NullType.nullable, "yyyy/MM/dd HH:mm:ss"));
		DataTable table = new JsonReader().read(listValue, new Table(TitleType.OnlyName, columnList));
		for (Map<String, Object> mapObject : table.getData()) {
			String secId = convertSecId((String) mapObject.get("secId"), true);
			mapObject.put("secId", secId);
			SmdcHandicap handicap = MapUtil.toBean(SmdcHandicap.class, mapObject);
			if (handicap.getTurnoverVol() == 0) {
				handicap.setOpen(false);
			}
			handicapList.add(handicap);
		}
		return handicapList;
	}
	
	@Override
	public List<SmdcMarketDaily> update(SmdcSecurity security, Date begin) {
		throw new RuntimeException("暂不支持");
	}

	@Override
	public List<SmdcMarketDaily> update(List<SmdcSecurity> securityList, List<Date> begin) {
		throw new RuntimeException("暂不支持");
	}

}
